package com.yykj.hospital.server.service.account.impl;

import com.yykj.hospital.server.dto.account.ExpendDTO;
import com.yykj.hospital.server.dto.account.IncomeDTO;
import com.yykj.hospital.server.enums.AccountLogType;
import com.yykj.hospital.server.enums.AccountType;
import com.yykj.hospital.server.enums.IncomeExpendType;
import com.yykj.hospital.server.model.entity.account.AccountDoctorLog;
import com.yykj.hospital.server.model.entity.account.AccountOrderInfo;
import com.yykj.hospital.server.model.entity.account.AccountSystemLog;
import com.yykj.hospital.server.model.entity.bank.DoctorBankCashApply;
import com.yykj.hospital.server.model.repository.account.AccountOrderInfoRepository;
import com.yykj.hospital.server.service.account.AccountService;
import com.yykj.hospital.server.service.account.DoctorAccountService;
import com.yykj.hospital.server.service.account.SystemAccountService;
import com.yykj.message.center.client.enums.MessageType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Override
    public void doctorFinishTaskIncome(IncomeDTO income) {
        AccountOrderInfo info = accountOrderInfoRepository.findFirstByOrderNoAndType(income.getOrderNum(), MessageType.TASK_FINISHED);
        if (info != null) {
            return;
        }

        AccountDoctorLog doctorAccountLog = new AccountDoctorLog();
        BeanUtils.copyProperties(income, doctorAccountLog);
        doctorAccountLog.setIncomeExpend(IncomeExpendType.income);

        AccountSystemLog accountSystemLog = new AccountSystemLog();
        BeanUtils.copyProperties(income, accountSystemLog);
        accountSystemLog.setIncomeExpend(IncomeExpendType.expend);
        accountSystemLog.setAccountType(AccountType.system);
        systemAccountService.expend(accountSystemLog);
        doctorAccountService.income(doctorAccountLog);
        info = new AccountOrderInfo();
        info.setType(MessageType.TASK_FINISHED);
        info.setOrderNo(income.getOrderNum());
        accountOrderInfoRepository.save(info);
    }

    @Override
    public void doctorCashOutPass(DoctorBankCashApply apply) {
        //提现金额账号减少提现申请的到账金额
        AccountSystemLog accountSystemLog = AccountSystemLog.builder()
                .hospitalId(apply.getHospitalId())
                .doctorId(apply.getDoctorId())
                .orderNum(apply.getOrderNo())
                .logType(AccountLogType.cash_out)
                .price(apply.getTotal())
                .detail("提现申请审核通过")
                .accountType(AccountType.cash)
                .incomeExpend(IncomeExpendType.expend)
                .build();
        systemAccountService.expend(accountSystemLog);

        //系统账户获取手续费
        AccountSystemLog accountSystemLog2 = AccountSystemLog.builder()
                .hospitalId(apply.getHospitalId())
                .doctorId(apply.getDoctorId())
                .orderNum(apply.getOrderNo())
                .logType(AccountLogType.cash_out)
                .price(apply.getApplyFee())
                .detail("提现手续费")
                .accountType(AccountType.system)
                .incomeExpend(IncomeExpendType.income)
                .build();
        systemAccountService.income(accountSystemLog2);
    }

    @Override
    public void doctorCashOutRefused(DoctorBankCashApply apply) {
        AccountDoctorLog doctorAccountLog = AccountDoctorLog.builder()
                .hospitalId(apply.getHospitalId())
                .doctorId(apply.getDoctorId())
                .orderNum(apply.getOrderNo())
                .logType(AccountLogType.cash_out)
                .incomeExpend(IncomeExpendType.income)
                .price(apply.getTotal())
                .detail("提现申请拒绝")
                .build();
        doctorAccountService.income(doctorAccountLog);

        AccountSystemLog accountSystemLog = AccountSystemLog.builder()
                .hospitalId(apply.getHospitalId())
                .doctorId(apply.getDoctorId())
                .orderNum(apply.getOrderNo())
                .logType(AccountLogType.cash_out)
                .price(apply.getTotal())
                .detail("提现申请拒绝")
                .accountType(AccountType.cash)
                .incomeExpend(IncomeExpendType.expend)
                .build();
        systemAccountService.expend(accountSystemLog);
    }

    @Override
    public void doctorCashOutApply(ExpendDTO expend) {

        AccountDoctorLog doctorAccountLog = new AccountDoctorLog();
        BeanUtils.copyProperties(expend, doctorAccountLog);
        doctorAccountLog.setIncomeExpend(IncomeExpendType.expend);

        AccountSystemLog accountSystemLog = new AccountSystemLog();
        BeanUtils.copyProperties(expend, accountSystemLog);
        accountSystemLog.setDetail("提现申请");
        accountSystemLog.setIncomeExpend(IncomeExpendType.income);
        accountSystemLog.setAccountType(AccountType.cash);

        doctorAccountService.expend(doctorAccountLog);
        systemAccountService.income(accountSystemLog);


    }

    @Override
    public void paySuccessAccount(IncomeDTO income) {

        AccountOrderInfo info = accountOrderInfoRepository.findFirstByOrderNoAndType(income.getOrderNum(), MessageType.PAY_SUCCESS);
        if (info != null) {
            return;
        }
        AccountSystemLog accountSystemLog = new AccountSystemLog();
        BeanUtils.copyProperties(income, accountSystemLog);
        accountSystemLog.setIncomeExpend(IncomeExpendType.income);
        accountSystemLog.setAccountType(AccountType.system);

        systemAccountService.income(accountSystemLog);

        info = new AccountOrderInfo();
        info.setType(MessageType.PAY_SUCCESS);
        info.setOrderNo(income.getOrderNum());
        accountOrderInfoRepository.save(info);

    }

    private DoctorAccountService doctorAccountService;

    private SystemAccountService systemAccountService;

    private AccountOrderInfoRepository accountOrderInfoRepository;

    @Autowired
    public void setDoctorAccountService(DoctorAccountService doctorAccountService) {
        this.doctorAccountService = doctorAccountService;
    }

    @Autowired
    public void setSystemAccountService(SystemAccountService systemAccountService) {
        this.systemAccountService = systemAccountService;
    }

    @Autowired
    public void setAccountOrderInfoRepository(AccountOrderInfoRepository accountOrderInfoRepository) {
        this.accountOrderInfoRepository = accountOrderInfoRepository;
    }
}
